package com.ruoyi.web.controller.purchase;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Purchase;
import com.ruoyi.purchase.service.PurchaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * @Author: 黄佳成
 * @Date: 2022/1/6
 */
@Controller
@RequestMapping("/purchase/check")
public class RepoCheckController extends BaseController {
    // 方法返回值前缀
    private String prefix = "purchase/check";
    @Autowired
    private PurchaseService purchaseService;

    // 品牌管理 左侧菜单栏
    @RequiresPermissions("purchase:check:view")
    @GetMapping()
    public String repo() {

        return prefix + "/check";
    }

    /**
     * 查询品牌信息列表
     */
    @RequiresPermissions("purchase:check:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Purchase purchase)
    {
        startPage();
        List<Purchase> list = purchaseService.queryListByStatus(purchase);
        return getDataTable(list);
    }

    /**
     * 导出品牌信息列表
     */
    @RequiresPermissions("purchase:check:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Purchase purchase)
    {
        List<Purchase> list = purchaseService.queryListByStatus(purchase);
        ExcelUtil<Purchase> util = new ExcelUtil<Purchase>(Purchase.class);
        return util.exportExcel(list, "purchase");
    }

    /**
     * 品牌信息信息  跳转到添加页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser user = getSysUser();//当前登录用户信息
        mmap.put("user",user);
        return prefix + "/add";
    }

    /**
     * 新增保存品牌信息
     */
    @RequiresPermissions("purchase:check:add")
    @Log(title = "品牌信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Purchase purchase) {
        return toAjax(purchaseService.save(purchase));
    }

    /**
     * 修改品牌信息 跳转到修改页面
     */
    @GetMapping("/check/{bid}")
    public String check(@PathVariable("bid") String PurchaseId, ModelMap mmap)
    {
        Purchase purchase = purchaseService.findPurchase(PurchaseId);
        mmap.put("purchase", purchase);
        return prefix + "/edit";
    }

    /**
     * 修改保存品牌信息
     */
    @RequiresPermissions("purchase:check:check")
    @Log(title = "采购单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Purchase purchase) throws ParseException {
        return toAjax(purchaseService.updatePurchaseby(purchase));
    }

    /**
     * 删除品牌信息
     */
    @RequiresPermissions("purchase:check:remove")
    @Log(title = "采购单信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(purchaseService.removeByIds(Arrays.asList(ids)));
    }

}
